﻿using Dapper;
using Estsh.Core.Base;
using Estsh.Core.Dapper;
using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Model.Result;
using Estsh.Core.Models;
using Estsh.Core.Services;
using Estsh.Core.Util;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Wms.IServices;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Estsh.Core.Wms.Services
{
    /// <summary>
    /// 收货查询补打模块管理业务处理类
    /// </summary>
    public class StockBarcodePrintService : BaseService<SysStock>, IStockBarcodePrintService
    {
        private readonly IStockBarcodePrintRepository repository;

        public StockBarcodePrintService(IStockBarcodePrintRepository _repository) : base(_repository)
        {
            repository = _repository;
        }
        #region 收货查询补打

        /// <summary>
        ///  根据分页条件获取列表
        /// </summary>
        /// <param name="account"></param>
        /// <param name="pager"></param>
        /// <param name="direction"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public Hashtable getListByPage(string cartonNo,string refOrderNo, string status, string lotno, string locateId, string zoneId, string beginCreateTime, string endCreateTime, int factoryId, Pager pager, String direction, String sort)
        {
            Hashtable result = new Hashtable();
            String strWhere = " 1=1 ";

            if (refOrderNo != null && !refOrderNo.Trim().Equals(""))
            {
                strWhere += "and a.ref_order_no like '%" + refOrderNo.Trim() + "%' ";
            }
            if (cartonNo != null && !cartonNo.Trim().Equals(""))
            {
                strWhere += "and a.carton_no like '%" + cartonNo.Trim() + "%' ";
            }
            if (status != null && !status.Trim().Equals(""))
            {
                strWhere += "and a.status = '" + status.Trim() + "' ";
            }
            if (lotno != null && !lotno.Trim().Equals(""))
            {
                strWhere += "and a.lot_no like '%" + lotno.Trim() + "%' ";
            }
            if (locateId != null && !locateId.Trim().Equals(""))
            {
                strWhere += "and a.locate_id ='" + locateId.Trim() + "' ";
            }
            if (zoneId != null && !zoneId.Trim().Equals(""))
            {
                strWhere += "and a.zone_id ='" + zoneId.Trim() + "' ";
            }
            if ((beginCreateTime != null && !beginCreateTime.Trim().Equals("")) && (endCreateTime != null && !endCreateTime.Trim().Equals("")))
            {
                strWhere += "and a.create_time between '" + beginCreateTime.Trim() + "' and '" + endCreateTime.Trim() + "' ";
            }
            if (!String.IsNullOrEmpty(beginCreateTime) && String.IsNullOrEmpty(endCreateTime))
            {
                strWhere += " and a.create_time > '" + beginCreateTime.Trim() + "'";
            }
            if (String.IsNullOrEmpty(beginCreateTime) && !String.IsNullOrEmpty(endCreateTime))
            {
                strWhere += " and a.create_time < '" + endCreateTime.Trim() + "'";
            }

            strWhere += " and a.factory_id = " + factoryId + " ";
            String orderBy = "";
            if (sort != null && !"".Equals(sort.Trim()))
            {
                orderBy += "a." + typeof(SysStock).GetEntityColumnName(sort.Trim()) + " " + direction;
            }
            else
            {
                orderBy += "a." + typeof(SysStock).GetEntityColumnName("Ruid") + " " + direction;
            }

            result = repository.getListByPage(pager.pageSize, pager.pageNo, strWhere, orderBy);
            return result;
        }

        //public Hashtable getListByPage(string cartonNo, string refOrderNo, string status, string lotno, string locateId, string zoneId, string beginCreateTime, string endCreateTime, int factoryId, Pager pager, string direction, string sort)
        //{
        //    throw new NotImplementedException();
        //}

        /// <summary>
        /// 获取库位列表
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public List<KeyValueResult> getLocateList()
        {
            return repository.getLocateList();
        }
        public List<KeyValueResult> getZoneList()
        {
            return repository.getZoneList();
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public String print(String ids)
        {
            ids = ids.Substring(0, ids.Length - 1);
            List<SysStock> dt = this.repository.getCartonListByRuid(ids);
            if (dt == null || dt.Count == 0)
            {
                return "Fail";
            }
            else
            {
                //return Printer.PrintSerialNumber(dt);
                return dt.Count.ToString();
            }
        }

        List<KeyValueResult> IStockBarcodePrintService.getLocateList()
        {
            throw new NotImplementedException();
        }

        List<KeyValueResult> IStockBarcodePrintService.getZoneList()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}